const formEle = this.getElement();
const baseForm = this.getApiPage().retrievePlugin('base-forms');
const prefix = this.pageObj.getVariables().find(v => v.name === "environmentType")?.value;
const $form = this.getApiPage().getApiWidgetByAliasName('judge-content')
const timeline = this.getApiPage().getApiWidgetByAliasName('timelineVertical')?.widgetObj;
let data = $form?.widgetObj._option.dataSource
const selfObj = this.widgetObj;
const options = {}
const requestGet = (url) => {
  const headers = { "Content-Type": "application/json" }
  return fetch(url, { headers }).then(res => res.json())
};
const requestPost = (url, body) => {
  const options = {
    method: 'POST',
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body)
  };
  return fetch(url, options).then(res => res.json())
};



const requestDict = (dictclassCode) => (
    requestGet(`/${prefix}Basics/basics-main/system/dictInfo/list?dictclassCode=${dictclassCode}`).then(({ success, result }) => {
      console.log(this, { success, result });
      if (success && result && Array.isArray(result.rows)) return result.rows.map(item => ({
        ...item, name: item.dictName, value: item.dataValue,
      }))
    })
)

function loadDict() {
  // 动态
  requestDict('event_trends_type').then((result) => {
    if (result) {
      selfObj.asyncSelectOptions('trends', result);
      options.eventTrendsType = result;
    }
  })
  // 所有时间
  requestDict('event_time_scope').then((result) => {
    if (result) {
      selfObj.asyncSelectOptions('timeScope', result);
      options.eventTimeScope = result;
    }
  })
  // 阶段
  requestDict('event_stage_type').then((result) => {
    if (result) {
      const eventStageType = result.reduce((def, item) => {
        if (['处置中', '未处置', '已处置', '已接警', '未接警'].includes(item.name)) {
          switch (item.name) {
            case '处置中': item.name = item.dictName = '演练中'; break;
            case '未处置': item.name = item.dictName = '未演练'; break;
            case '已处置': item.name = item.dictName = '已演练'; break;
            case '未接警': item.name = item.dictName = '未编制'; break;
            case '已接警': item.name = item.dictName = '已编制'; break;
          }
          def.push(item)
        } else {
          if (item.name.indexOf('接警') === -1 && item.name.indexOf('研判') === -1) {
            def.push(item)
          }
        }

        return def
      }, [])
      selfObj.asyncSelectOptions('stage', eventStageType)
      options.eventStageType = eventStageType;
    }
  })
}

// 加载数据字典
loadDict();

$(formEle).on('click', '.layui-btn-search', () => {
  const $form = this.getApiPage().getApiWidgetByAliasName('judge-content')?.widgetObj;
  let data = $form._option.dataSource;
  // 重置表单
  baseForm.resetForm('form_record');
  // 重置表单
  selfObj?.setFormData('form_record', {});
  selfObj?.render();
  selfObj?.asyncSelectOptions('trends', options.eventTrendsType);
  selfObj?.asyncSelectOptions('timeScope', options.eventTimeScope);
  selfObj?.asyncSelectOptions('stage', options.eventStageType);

  if (data) {
    requestPost(`/${prefix}SummaryAssessmentSV/emergencyRecord/planHandleProcessDetailByReq`, { eventId: data.id, queryType: '1' }).then(({ result, success }) => {
      if (success && Array.isArray(result)) {
        timeline.dataSource = result;
        timeline.render();
      }
      console.log('事件记录： =》 ', { result, success })
    })
  }
})


